package com.leyou.contrlloer;

import com.leyou.domian.SpuDetail;
import com.leyou.dto.PageResult;
import com.leyou.dto.SkuDTO;
import com.leyou.dto.SpuDTO;
import com.leyou.dto.SpuDetailDTO;
import com.leyou.service.GoodsService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RequestMapping
@RestController
public class GoodsController {

    @Autowired
    GoodsService goodsService;


    @GetMapping("spu/page")
    public ResponseEntity<PageResult<SpuDTO>> findGoodsPage(@RequestParam(value = "page", defaultValue = "1") Integer pages,
                                                            @RequestParam(value = "rows", defaultValue = "5") Integer rows,
                                                            @RequestParam(value = "key", required = false) String key,
                                                            @RequestParam(value = "key", required = false) Boolean saleable) {


        PageResult<SpuDTO> pageResult = goodsService.findGoodsPage(pages, rows, key, saleable);
        return ResponseEntity.ok(pageResult);
    }


    @PostMapping("goods")
    public ResponseEntity<Void> saveGood(@RequestBody SpuDTO spuDTO) {

        goodsService.saveGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.CREATED).build();
    }

    @PutMapping("goods")
    public ResponseEntity<Void> updateGoods(@RequestBody SpuDTO spuDTO) {
        goodsService.updateGoods(spuDTO);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @PutMapping("spu/saleable")
    ResponseEntity<Void> updateSaleable(@RequestParam("id") Long id, @RequestParam("saleable") Boolean saleable) {
        goodsService.updateSaleable(id, saleable);
        return ResponseEntity.status(HttpStatus.NO_CONTENT).build();
    }

    @GetMapping("spu/detail")
    ResponseEntity<SpuDetailDTO> findSpuDetail(@RequestParam("id") Long id) {
        SpuDetailDTO spuDetail = goodsService.findSpuDetail(id);
        return ResponseEntity.ok(spuDetail);
    }

    @GetMapping("sku/of/spu")
    ResponseEntity<List<SkuDTO>> findSKus(@RequestParam("id") Long id) {
        List<SkuDTO> skuDTOList = goodsService.findSKus(id);
        return ResponseEntity.ok(skuDTOList);
    }

}
